const app = getApp();
import {
  getMessage,
  getMessagedetail,
  getPrivateMessage,
  postMessages,
  getMessageslist
} from "../api/message";

/**
 * 获取消息列表
 */

const getMessages = function (args) {
  let that = this;
  if (!this.data.user) {
    wx.navigateTo({
      url: '/pages/login/login',
    })
    return
  }
  getMessage(args).then(res => {
    if (res.length < 10) {
      this.setData({
        isLastPage: true,
        loadtext: '到底啦'
      })
    }
    args.messageList = [].concat(this.data.messageList, res)
    args.page = this.data.page + 1
    this.setData(args)
  }).catch(err => {
    console.log(err);
    if (err.message == "授权登录信息错误, 无法获取用户信息") {
      that.data.currentConfig.show = true;
      that.setData({
        currentConfig: that.data.currentConfig,
      })
    }
  })
}


/**
 * 跳转详情页
 */
const onDialogItemClick = function (e) {
  let id = e.currentTarget.id,
    type = e.currentTarget.dataset.type,
    userid = e.currentTarget.dataset.userid,
    name = e.currentTarget.dataset.name;
  wx.navigateTo({
    url: '/pages/message/detail/detail?id=' + id + '&type=' + type + '&userid=' + userid + '&name=' + name
  })
}

/**
 * 获取消息内容
 */
const getMessagedetails = function (data) {
  let that = this;
  getMessagedetail(data).then(res => {
    console.log(res);
    that.setData({
      recive: res.user,
      content: res.content
    })
  })
}

const getPrivatemessage = function (args) {
  let that = this;
  getPrivateMessage(args).then(res => {
    console.log(res);
    if (res.length < 10) {
      this.setData({
        isLastPage: true,
        loadtext: '到底啦'
      })
    }
    args.messageList = [].concat(this.data.messageList, res)
    args.page = this.data.page + 1
    this.setData(args)
  }).catch(err => {
    console.log(err);
    if (err.message == "授权登录信息错误, 无法获取用户信息") {
      that.data.currentConfig.show = true;
      that.setData({
        currentConfig: that.data.currentConfig,
      })
    }
  })
}


/**
 * 发送私信
 */
const postMessage = function () {
  let that = this,
    content = that.data.content,
    receive = that.data.receive,
    id = that.data.id;
  if (content == "" || content == undefined) {
    wx.showToast({
      title: '您还未填写私信内容',
    })
    return
  }
  let args = {
    receive: receive,
    title: content,
    id: id,
    content: content
  }
  postMessages(args).then(res => {
    console.log(res);
    this.setData({
      reply: '',
      content: '',
      messageList:[],
      page:1,
      emojiShow: false,
    })
    that.getPrivatemessage({
      page:that.data.page,
      receive: receive,
    })
  })


}

/**
   * 获取我收藏的
   */

 const  getUserPosts= function(args) {
    let that = this;
    getMessageslist(args).then(res => {
        let args = {}
        if (res.length < 10) {
          this.setData({
            isLastPage: true,
            loadtext: '到底啦'
          })
        }
        if (this.data.isPull) {
          this.setData({
            isPull: false
          })
          wx.stopPullDownRefresh()
        } else if (this.data.isBottom) {
          args.posts = [].concat(this.data.posts, res)
          args.page = this.data.page + 1
        } else {
          args.posts = res;
          args.page = this.data.page + 1
        }
        this.setData(args);
        wx.hideLoading()
      })
      .catch(err => {
        if (err.message = "授权信息有误") {
          that.setData({
            showlogin: true,
            nologin: true
          })
        }
      })
  }
  const tapNavigateTo= function(e) {
    let type = e.currentTarget.dataset.type;
    let id = e.currentTarget.id;
    console.log(type);
    wx.navigateTo({
      url: '/pages/detail/detail?id='+id+'&type='+type,
    })
  }

module.exports = function (obj) {
  obj.getMessages = getMessages;
  obj.onDialogItemClick = onDialogItemClick;
  obj.getPrivatemessage = getPrivatemessage;
  obj.postMessage = postMessage;
  obj.getUserPosts=getUserPosts;
  obj.tapNavigateTo=tapNavigateTo
}